using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Countdown
{
    public partial class CountdownForm : Form
    {
        // Used for form move
        private Point mouseOffset;

        private DateTime countdownDate = DateTime.MinValue;
        private SettingsForm parentForm;
        private string eventName;
        private bool constrainToRight = false;

        public CountdownForm( SettingsForm parent )
        {
            InitializeComponent();

            parentForm = parent;
        }

        private void CountdownForm_Load(object sender, EventArgs e)
        {
            ResizeForm();
        }

        /// <summary>
        /// Used to force the form to only be positioned
        /// along the right side of the screen.
        /// </summary>
        public bool ConstrainToRightSide
        {
            get { return constrainToRight; }
            set { constrainToRight = value; }
        }

        /// <summary>
        /// Provides the name of the event for display.
        /// </summary>
        public string EventName
        {
            get{ return eventName; }
            set{ eventName = value; }
        }

        /// <summary>
        /// The date of the countdown event.
        /// </summary>
        public DateTime EventDate
        {
            get{ return countdownDate; }
            set
            {
                countdownDate = value;
                countdownTimer.Enabled = true;
            }
        }

        /// <summary>
        /// Size the form based on the current label size.
        /// Grows/shrinks dynamically based on the current
        /// message being displayed.
        /// </summary>
        private void ResizeForm()
        {
            this.Height = countdownLabel.Height;
            this.Width = countdownLabel.Width;

            if (constrainToRight)
            {
                int screenWidth =
                    System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;

                this.Left = screenWidth - this.Width;
            }
        }

        /// <summary>
        /// Ticks once every second.  Upon tick event,
        /// checks to see if the specified time has
        /// been reached.  Either updates the countdown
        /// string, or displays the event name.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void countdownTimer_Tick(object sender, EventArgs e)
        {
            if (countdownDate == null || countdownDate == DateTime.MinValue)
            {
                // Do nothing.  No date set.
            }
            else if (countdownDate.CompareTo(DateTime.Now) <= 0)
            {
                // The date has been reached.  This stops the
                // timer and displays the event name.
                countdownLabel.Text = eventName;
                parentForm.EventReached();

                countdownTimer.Enabled = false;
            }
            else
            {
                // Builds up the number of days, hours, minutes,
                // and seconds until the event.
                StringBuilder timerString = new StringBuilder();

                TimeSpan diff = countdownDate.Subtract(DateTime.Now);

                if (diff.Days > 0)
                {
                    timerString.Append(diff.Days);
                    timerString.Append(" days, ");
                }

                if( diff.Hours > 0 )
                {
                    timerString.Append(diff.Hours);
                    timerString.Append(" hours, ");
                }

                if (diff.Minutes > 0)
                {
                    timerString.Append(diff.Minutes);
                    timerString.Append(" minutes, ");
                }

                timerString.Append(diff.Seconds);
                timerString.Append(" seconds");

                countdownLabel.Text = timerString.ToString();

                parentForm.UpdateTooltip(timerString.ToString());
            }

            ResizeForm();
        }

        /// <summary>
        /// When the mouse is clicked on the label (which
        /// fills the entire form), the coordinates are
        /// captured to use as offsets to move the form
        /// as the mouse is moved.
        /// 
        /// Addition information on moving nonstandard form:
        /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchShapedWindowsFormsControlsInVisualStudioNET.asp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void countdownLabel_MouseDown(object sender, MouseEventArgs e)
        {
            mouseOffset = new Point(-e.X, -e.Y);
        }

        /// <summary>
        /// Responds to the mouse moving, as long as the
        /// left mouse button is also down.  For each
        /// movement, the form is moved correspondingly.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void countdownLabel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;

                if (constrainToRight)
                {
                    this.Top = Control.MousePosition.Y + mouseOffset.Y;
                }
                else
                {
                    mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                    this.Location = mousePos;
                }
            }
        }

        /// <summary>
        /// When the mouse left button is released, the
        /// new form coordinates are saved.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void countdownLabel_MouseUp(object sender, MouseEventArgs e)
        {
            // Update the location of the window in settings
            Properties.Settings.Default.Location = this.Location;
            Properties.Settings.Default.Save();
        }

    }
}